package com.arm.controller.api.admin.menu;

import com.arm.controller.BaseController;
import com.arm.def.Constant;
import com.arm.module.AdminMenu;
import com.arm.module.AdminUser;
import com.arm.module.admin.Menu;
import com.arm.service.MenuService;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import com.arm.tools.result.ResultObj;
import com.arm.tools.result.ReturnCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Baggio on 2019-1-30.
 */
@Api(description = "后台菜单接口")
@RestController
@RequestMapping(value = "/api/admin/menu")
public class MenuController extends BaseController {
    @Autowired
    MenuService menuService;

    @ApiOperation(value = "获取菜单列表", notes = "获取菜单列表")
    @GetMapping(value = "/listByRole")
    public ResultObj<List<Menu>> getMenuList(HttpServletRequest request) {

        ResultObj resultObj = new ResultObj();
        try {


            AdminUser adminUser=(AdminUser)super.session.getAttribute(Constant.SESSION_KEY_ADMIN_USER_LOGIN);
        if(adminUser==null){
            throw new MyRunTimeExcption("adminUser is null");
        }
            List<Menu> menuList = menuService.getMenuListByRole(new HashMap<String,String>(){{
                put("tid",String.valueOf(adminUser.getTid()));
            }});

            resultObj.setData(menuList);
            resultObj.setMsg("成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
logger.info(resultObj.getData().toString());

        return resultObj;

    }

    @ApiOperation(value = "获取全部菜单列表", notes = "获取菜单列表")
    @GetMapping(value = "/all")
    public ResultObj<List<Menu>> getALLMenuList() {

        ResultObj resultObj = new ResultObj();
        try {
            List<Menu> menuList = menuService.getALLMenuList(null);
            resultObj.setData(menuList);
            resultObj.setMsg("成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        logger.info(resultObj.getData().toString());

        return resultObj;

    }


    @ApiOperation(value = "添加菜单", notes = "添加菜单")
    @PostMapping(value="/")
    public ResultObj addMenu(@RequestBody  AdminMenu adminMenu) {
        System.out.println("=================================addMenu==========================================>>>>>>");
        ResultObj resultObj = new ResultObj();
        try {

            resultObj.setMsg("成功！");
            menuService.addMenu(adminMenu);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj;
    }
}
